package tech.aistar.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import tech.aistar.mapper.UserInfoMapper;
import tech.aistar.pojo.User;
import tech.aistar.pojo.UserInfo;
import tech.aistar.pojo.vo.UserInfoVo;
import tech.aistar.service.OrderService;
import tech.aistar.service.UserInfoService;
import tech.aistar.service.UserService;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User selectById(int userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public int updateById(UserInfo user) {
        return userInfoMapper.updateById(user);
    }

    @Override
    public int add(UserInfo user) {
        return userInfoMapper.add(user);
    }


    @Override
    public User selectByEmail(String email) {
        return userInfoMapper.selectByEmail(email);
    }

    @Override
    public void sendCode(String email) {
        //TODO... 发送验证码的逻辑
        //1. 封装邮件的各个信息的
        SimpleMailMessage msg = new SimpleMailMessage();
        //邮件的发送方,此处的邮箱必须是application.yml中设置的邮箱
        msg.setFrom("3196287362@qq.com");
        //设置邮件的标题
        msg.setSubject("青岛科技验证码");
        //随机生成一个[100000,999999]之间的6位的随机整数
        String code = String.format("%06d", (int) (Math.random() * 900000) + 100000);
        //设置邮件的正文内容
        msg.setText("验证码:+" + code);
        //邮件的接收方 - 是可以随便写的
        msg.setTo(email);
        //发送
        mailSender.send(msg);
        redisTemplate.opsForValue().set(email, code, 3, TimeUnit.MINUTES);
    }


    @Override
    public int updatePwdByEmail(String email, String pwd) {
        User user = new User();
        user.setEmail(email);
        pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
        user.setPassword(pwd);
        return userInfoMapper.updatePwdByEmail(user);
    }


    @Override
    @Transactional
    public int updateUserByUser_id(Integer userId, String name,String gender,String phone,String address,String introduction) {
        int i = 0;
        User user = new User();
        user.setUserId(userId);
        user.setUsername(name);
        user.setPhone(phone);
        i = userInfoMapper.updateUserByUser_id(user);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setSex(gender);
        userInfo.setAddress(address);
        userInfo.setIntroduction(introduction);
        i += userInfoMapper.updateInfoByUser_id(userInfo);
        return i;
    }

    @Override
    public UserInfoVo selectInfoByUser_id(int userId) {
        return userInfoMapper.selectInfoByUser_id(userId);
    }


}
